layui.define(['jquery'], function (exports) {
    const {jquery: $} = layui;
    exports("part", {

        render: function (win = undefined) {
            var selector = function (rule) {
                if (win) {
                    return $(win).find(rule);
                }

                return $(rule);
            }
            //动态渲染Part
            selector("[lay-part]").each(function (idx, elem) {
                const partName = selector(elem).attr("lay-part");
                const partMethod = selector(elem).data("method") || "render";
                const data = selector(elem).data();

                data.elem = elem;

                var chain = layui;

                if (!layui.modules[partName]) {
                    //避免重复设置模块的地址，防止提示 * Module already exists
                    const options = {};
                    options[partName] = '{/}/part/' + partName;
                    chain = chain.extend(options);
                }

                chain.use([partName], function () {
                    const part = layui[partName];
                    part[partMethod](data);
                });
            });
        },
    })
});